package com.sugon.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.SysLogDao;
import com.sugon.entity.SysUserEntity;
import com.sugon.entity.dto.LogDTO;
import com.sugon.service.BaseCommonService;
import com.sugon.utils.IPUtils;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Description: common实现类
 * @author: YuXD
 */
@Service
@Slf4j
public class BaseCommonServiceImpl implements BaseCommonService {

    @Resource
    private SysLogDao sysLogDao;

    @Override
    public void addLog(LogDTO logDTO) {
        if (StrUtil.isEmpty(logDTO.getId())) {
            logDTO.setId(RandomUtil.randomString(32));
        }
        //保存日志（异常捕获处理，防止数据太大存储失败，导致业务失败）JT-238
        try {
            logDTO.setCreateTime(new Date());
            sysLogDao.saveLog(logDTO);
        } catch (Exception e) {
            log.warn(" LogContent length : " + logDTO.getLogContent().length());
            log.warn(e.getMessage());
        }
    }

    @Override
    public void addLog(String busModule, String logContent, Integer logType, Integer operateType, String operateClient) {
        LogDTO sysLog = new LogDTO();
        sysLog.setId(IdUtil.fastUUID());
        //注解上的描述,操作日志内容
        sysLog.setBusModule(busModule);
        sysLog.setLogContent(logContent);
        sysLog.setLogType(logType);
        sysLog.setOperateType(operateType);
        sysLog.setOperateClient(operateClient);
        try {
            //获取request
            HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
            //设置IP地址
            sysLog.setIp(IPUtils.getIpAddr(request));
        } catch (Exception e) {
            sysLog.setIp("127.0.0.1");
        }
        sysLog.setUserid(ShiroUtils.getUserId());
        sysLog.setUsername(ShiroUtils.getUserName());
        sysLog.setCreateTime(new Date());
        //保存日志（异常捕获处理，防止数据太大存储失败，导致业务失败）JT-238
        addLog(sysLog);
    }

    @Override
    public void addLog(String busModule,String logContent, Integer logType, Integer operateType, String operateClient, SysUserEntity user) {
        LogDTO sysLog = new LogDTO();
        sysLog.setId(IdUtil.fastUUID());
        //注解上的描述,操作日志内容
        sysLog.setBusModule(busModule);
        sysLog.setLogContent(logContent);
        sysLog.setLogType(logType);
        sysLog.setOperateType(operateType);
        sysLog.setOperateClient(operateClient);
        try {
            //获取request
            HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
            //设置IP地址
            sysLog.setIp(IPUtils.getIpAddr(request));
        } catch (Exception e) {
            sysLog.setIp("127.0.0.1");
        }
        sysLog.setUserid(user.getUserId());
        sysLog.setUsername(user.getUserName());
        sysLog.setCreateTime(new Date());
        //保存日志（异常捕获处理，防止数据太大存储失败，导致业务失败）JT-238
        addLog(sysLog);
    }

    //@Override
    //public void addLog(String logContent, Integer logType, Integer operatetype, LoginUser user) {
    //    LogDTO sysLog = new LogDTO();
    //    sysLog.setId(String.valueOf(IdWorker.getId()));
    //    //注解上的描述,操作日志内容
    //    sysLog.setLogContent(logContent);
    //    sysLog.setLogType(logType);
    //    sysLog.setOperateType(operatetype);
    //    try {
    //        //获取request
    //        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
    //        //设置IP地址
    //        sysLog.setIp(IpUtils.getIpAddr(request));
    //    } catch (Exception e) {
    //        sysLog.setIp("127.0.0.1");
    //    }
    //    //获取登录用户信息
    //    if(user==null){
    //        try {
    //            user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
    //        } catch (Exception e) {
    //            //e.printStackTrace();
    //        }
    //    }
    //    if(user!=null){
    //        sysLog.setUserid(user.getUserName());
    //        sysLog.setUsername(user.getNickName());
    //    }
    //    sysLog.setCreateTime(new Date());
    //    //保存日志（异常捕获处理，防止数据太大存储失败，导致业务失败）JT-238
    //    try {
    //        baseCommonMapper.saveLog(sysLog);
    //    } catch (Exception e) {
    //        log.warn(" LogContent length : "+sysLog.getLogContent().length());
    //        log.warn(e.getMessage());
    //    }
    //}
    //
    //@Override
    //public void addApiLog(String logContent, String method, String requestUrl, String requestParam, String responseContent) {
    //    LogDTO sysLog = new LogDTO();
    //    sysLog.setId(String.valueOf(IdWorker.getId()));
    //    //注解上的描述,操作日志内容
    //    sysLog.setLogContent(logContent);
    //    sysLog.setLogType(CommonConstant.LOG_TYPE_3);
    //    sysLog.setOperateType(CommonConstant.OPERATE_TYPE_7);
    //    // API调用时请求方法和类型一致
    //    sysLog.setMethod(method);
    //    sysLog.setRequestType(method);
    //    sysLog.setRequestUrl(requestUrl);
    //    sysLog.setRequestParam(requestParam);
    //    sysLog.setResponseContent(responseContent);
    //    try {
    //        //获取request
    //        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
    //        //设置IP地址
    //        sysLog.setIp(IpUtils.getIpAddr(request));
    //    } catch (Exception e) {
    //        sysLog.setIp("127.0.0.1");
    //    }
    //    sysLog.setUserid(CommonUtil.getLoginUserName());
    //    sysLog.setUsername(CommonUtil.getLoginUserNickName());
    //    sysLog.setCreateTime(DateUtil.date());
    //    //保存日志（异常捕获处理，防止数据太大存储失败，导致业务失败）JT-238
    //    try {
    //        baseCommonMapper.saveLog(sysLog);
    //    } catch (Exception e) {
    //        log.warn(" LogContent length : "+sysLog.getLogContent().length());
    //        log.warn(e.getMessage());
    //    }
    //}
    //
    //@Override
    //public void addLog(String logContent, Integer logType, Integer operateType) {
    //    addLog(logContent, logType, operateType, null);
    //}

}
